package com.gf.framework.rediscachedemo.controller;

import com.alibaba.excel.annotation.ExcelProperty;
import com.gf.framework.common.utils.list.GfLinkedList;
import com.gf.framework.redis.annotation.RedissonLock;
import com.gf.framework.redis.annotation.RepeatRequest;
import com.gf.framework.redis.core.RedisClient;
import com.gf.framework.web.annotation.ExportExcel;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.redisson.api.RLock;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gaofei
 * @date 2022/9/8 21:33
 */
@RestController
@Api(tags = "redis lock")
public class LocalController {

    @Resource
    RedisClient redisClient;

    @GetMapping("locktest")
    public Boolean lockTest() throws Exception {
        RLock rLock = redisClient.getLock("111");
        if (rLock.tryLock()) {
            newThread(rLock);
        }
        else {
            throw new Exception("不允许重复操作");
        }
        return Boolean.TRUE;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Student {
        @ExcelProperty("名字")
        private String name;
        @ExcelProperty("年龄")
        private Integer age;
    }

    @ExportExcel
    @GetMapping("exporttest")
    public List<Student> export() {
        return new GfLinkedList<Student>().of(
                new Student("123", 1),
                new Student("123445", 2),
                new Student("gweegw", 3));
    }

    private void newThread(RLock rLock) {
        new Thread(()->{
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            rLock.unlock();
        }).start();
    }

}
